package com.smtech.xz.oa;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.smtech.xz.util.ImageUtils;
import com.smtech.xz.util.SPUtils;

import java.io.InputStream;

import static com.smtech.xz.api.URLConstant.SPLASH;
import static com.smtech.xz.util.ImageUtils.PATH;
import static com.smtech.xz.util.SPUtils.FIRST_BOOT;
import static com.smtech.xz.util.SPUtils.IMG_URL;

/**
 * @auth rou
 * @time 2018/1/12
 * @desc
 */

public class StartActivity extends BaseActivity {

    private ImageView iv;
    private TextView tv;
    private String path, totalPath;
    private Bitmap bitmap;
    private MyCountDownTimer mCountDownTimer;
//    Handler tmpHandler = new Handler();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){
            finish();
            return;
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

//        initMonitor(getApplication());
        LocationHelper.initGaoDe(getApplicationContext());


        iv = findViewById(R.id.image);
        path = SPUtils.get(IMG_URL, "");
        if (!TextUtils.isEmpty(path)) {
            totalPath = Environment.getExternalStorageDirectory().getAbsolutePath() + PATH + path;
            bitmap = ImageUtils.getLoacalBitmap(totalPath);
        }
        if (bitmap != null) {
            iv.setImageBitmap(bitmap);
        } else {
            Drawable drawable = getResources().getDrawable(R.drawable.img_start, null);
            iv.setImageDrawable(drawable);
        }
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCountDownTimer != null) {
                    mCountDownTimer.cancel();
                }
                startMain(2);
//                tmpHandler.postDelayed(runnable, 1000);

            }
        });
        tv = findViewById(R.id.start_skip_count_down);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startMain(0);
            }
        });

        String firstBoot = SPUtils.get(FIRST_BOOT, "");
        if (!"".equals(firstBoot)) {
            //创建倒计时类
            mCountDownTimer = new MyCountDownTimer(5000, 1000);
            mCountDownTimer.start();
//            tmpHandler.postDelayed(runnable, 5000);
        } else {
            startMain(0);
            return;
        }

    }


    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            startMain(0);
        }
    };

    @Override
    protected void onDestroy() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }
        super.onDestroy();
    }


    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture    表示以「 毫秒 」为单位倒计时的总数
         *                          例如 millisInFuture = 1000 表示1秒
         * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick()
         *                          例如: countDownInterval = 1000 ; 表示每 1000 毫秒调用一次 onTick()
         */

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }


        @Override
        public void onFinish() {
            startMain(0);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv.setText(millisUntilFinished / 1000 + "s 跳过");
        }

    }

    private void startMain(int i) {
        Intent intent = new Intent(StartActivity.this, MainActivity.class);
        if (i == 0) {
            intent.putExtra(SPLASH, "0");
            startActivity(intent);
        } else if (i == 2) {
            intent.putExtra(SPLASH, "2");
            startActivity(intent);
        }
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        //结束的动画
        overridePendingTransition(R.anim.fade, R.anim.hold);
    }

    /**
     * todo 同步 app要使用
     *
     * @param application
     */
    /*private void initMonitor(final Application application) {
        List<Priority> priorities = new ArrayList<>();
        priorities.add(Priority.ERROR);
        final Config config = new Config.Builder()
                .isDebug(BuildConfig.DEBUG)
                .priority(priorities)
                .build();
        final BaseInfo baseInfo = new BaseInfo.Builder()
                .build();
        InitParams initParams = new InitParams() {
            @NotNull
            @Override
            public BaseInfo baseInfo() {
                return baseInfo;
            }

            @NotNull
            @Override
            public Config config() {
                return config;
            }

            @NotNull
            @Override

            public String userId() {
                return "OA";
            }

            @NotNull
            @Override
            public String channel() {
                return "OA";
            }
        };
        MSTMonitor.init(application, initParams);

    }*/


}
